package com.xiaohujr.xuexue.leetcode.interview;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author : quzhixin
 * @create 2020/02/02 1:21 下午
 * @description: 加减库存
 */
public class ProducerConsumerZhiXin {
    private AtomicInteger count = new AtomicInteger(0);
    private static final int MAX_COUNT = 10;
    private static final int MIN_COUNT = 0;
    private volatile boolean isProduce = true;
    private volatile boolean stopConsumer = false;

    private AtomicInteger totalConsume = new AtomicInteger(0);
    private Lock cosumeLock=new ReentrantLock();
    private Condition condition = cosumeLock.newCondition();

    private Runnable addStorage = () -> {
        while (true) {
            while (this.isProduce) {
                if (this.count.intValue() < ProducerConsumerZhiXin.MAX_COUNT) {
                    System.out.println(Thread.currentThread().getName() + "生产一个资源，仓库储量为" + count.incrementAndGet());
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    this.isProduce = false;
                }
            }
        }
    };

    private Runnable decStorage = new Runnable() {

        @Override
        public void run() {
            while (true) {
                while (!isProduce && !stopConsumer) {
                    cosumeLock.lock();
                    condition.signal();
                    try{
                        if (count.intValue() > MIN_COUNT) {
                            System.out.println(Thread.currentThread().getName() + "消费一个资源，仓库储量为" + count.decrementAndGet());
                            totalConsume.addAndGet(1);
                            Thread.sleep(300);
                            if(totalConsume.get()>=30){
                                stopConsumer=true;
                                condition.await();
                            }
                        } else {
                            isProduce = true;
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        cosumeLock.unlock();
                    }
                }
            }
        }

    };

    public static void main(String[] args) {
        ProducerConsumerZhiXin producerConsumer = new ProducerConsumerZhiXin();
        Thread threadProducerA = new Thread(producerConsumer.addStorage, "ProducerT1");
        Thread threadProducerB = new Thread(producerConsumer.addStorage, "ProducerT2");
        Thread threadConsumerA = new Thread(producerConsumer.decStorage, "ConsumerT1");
        Thread threadConsumerB = new Thread(producerConsumer.decStorage, "ConsumerT2");

        threadProducerA.start();
        threadProducerB.start();
        threadConsumerA.start();
        threadConsumerB.start();
    }
}